//
//  JQFYRhythmView.m
//  JQFY
//
//  Created by apple on 2017/8/8.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYRhythmView.h"

@implementation JQFYRhythmView
{
    UIView *_line1;
    UIView *_line2;
    UIView *_line3;
    UIView *_line4;
    
    NSLayoutConstraint *_lineConstraint1;
    NSLayoutConstraint *_lineConstraint2;
    NSLayoutConstraint *_lineConstraint3;
    NSLayoutConstraint *_lineConstraint4;
    
    BOOL _isInitialState;
    BOOL _isAnimation;
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
-(void)setupUI{

        _isInitialState = NO;
        _isAnimation = YES;
        
        _line1 = [[UIView alloc] init];
        _line2 = [[UIView alloc] init];
        _line3 = [[UIView alloc] init];
        _line4 = [[UIView alloc] init];
        
        [self addSubview:_line1];
        [self addSubview:_line2];
        [self addSubview:_line3];
        [self addSubview:_line4];
        
        self.lineWidth = 2 *SCAL;
        self.lineColor = [UIColor whiteColor];
        
        _line1.backgroundColor = self.lineColor;
        _line2.backgroundColor = self.lineColor;
        _line3.backgroundColor = self.lineColor;
        _line4.backgroundColor = self.lineColor;
        
        _line1.layer.cornerRadius = 1;
        _line2.layer.cornerRadius = 1;
        _line3.layer.cornerRadius = 1;
        _line4.layer.cornerRadius = 1;
        
        [_line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.5* SCAL);
            make.left.equalTo(self.mas_left);
            make.width.offset(self.lineWidth);
            make.bottom.equalTo(self.mas_bottom);
        }];
        
        
        [_line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(self.bounds.size.height*0.2* SCAL);
            make.left.equalTo(_line1.mas_right).offset((self.bounds.size.width-(self.lineWidth*4* SCAL))/3* SCAL);
            make.width.offset(self.lineWidth);
            make.bottom.equalTo(self);
        }];
        
        [_line3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(self.bounds.size.height*0.4* SCAL);
            make.left.equalTo(_line2.mas_right).offset((self.bounds.size.width-(self.lineWidth*4* SCAL))/3* SCAL);
            make.width.offset(self.lineWidth);
            make.bottom.equalTo(self);
        }];
        
        [_line4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(self.bounds.size.height*0.3* SCAL);
            make.left.equalTo(_line3.mas_right).offset((self.bounds.size.width-(self.lineWidth*4* SCAL))/3* SCAL);
            make.width.offset(self.lineWidth);
            make.bottom.equalTo(self);
        }];
}

- (void)beginAnimation
{
    _isAnimation = YES;
    [self animation];
    self.animation = YES;
}

- (void)stopAnimation
{
    _isAnimation = NO;
    [self animation];
    self.animation = NO;
}

- (void)setLineWidth:(float)lineWidth
{
    _lineWidth = lineWidth;
    [_line1 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(lineWidth);
    }];
    [_line2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(lineWidth);
    }];
    [_line3 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(lineWidth);
    }];
    [_line4 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(lineWidth);
    }];
}

- (void)setLineColor:(UIColor *)lineColor
{
    _lineColor = lineColor;
    _line1.backgroundColor = lineColor;
    _line2.backgroundColor = lineColor;
    _line3.backgroundColor = lineColor;
    _line4.backgroundColor = lineColor;
}

- (void)animation
{
    if (_isAnimation) {
        if (_isInitialState) {
            [_line1 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.1* SCAL);
            }];
            [_line2 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.6* SCAL);
            }];
            [_line3 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.0* SCAL);
            }];
            [_line4 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.7* SCAL);
            }];
        }
        else {
            [_line1 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.5* SCAL);
            }];
            [_line2 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.2* SCAL);
            }];
            [_line3 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.4* SCAL);
            }];
            [_line4 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.3* SCAL);
            }];
        }
        
        [UIView animateWithDuration:0.3 animations:^{
            [self layoutIfNeeded];
        } completion:^(BOOL finished) {
            _isInitialState = !_isInitialState;
            [self animation];
        }];
    }
    else {
        [_line1 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.3* SCAL);
        }];
        [_line2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.5* SCAL);
        }];
        [_line3 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.2* SCAL);
        }];
        [_line4 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(self.bounds.size.height*0.6* SCAL);
        }];
        [self layoutIfNeeded];
    }
}

@end
